package com.techsoul.module.system.config;

import com.bstek.ureport.console.UReportServlet;
import com.bstek.ureport.definition.datasource.BuildinDatasource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Slf4j
@ImportResource("classpath:context.xml")
@Configuration
public class UReportConfig implements BuildinDatasource {

    @Autowired
    private DataSource dataSource;

    @Bean
    public ServletRegistrationBean registrationBean() {
        return new ServletRegistrationBean(new UReportServlet(), "/ureport/*");
    }

    @Override
    public String name() {
        return "UReport数据源";
    }

    @Override
    public Connection getConnection() {
        // 获取配置的数据源
        Connection connection= null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }
}
